package com.th.supcom.pds.phamapply.server;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.carefx.framework.container.client.exception.UiException;
import net.carefx.framework.exception.BusinessException;
import net.carefx.framework.util.ConvertUtils;
import net.carefx.framework.web.gwt.server.GwtController;

import org.dozer.Mapper;

import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.th.supcom.pds.domain.pojo.InpPhamApplyDetailSummary;
import com.th.supcom.pds.domain.pojo.InpPhamApplyMaster;
import com.th.supcom.pds.domain.pojo.InpPhamApplyPatInfo;
import com.th.supcom.pds.domain.vo.InpPhamApplyDetailVO;
import com.th.supcom.pds.internal.api.phamapply.PhamApplyService;
import com.th.supcom.pds.phamapply.client.model.InpPhamApplyDetailModel;
import com.th.supcom.pds.phamapply.client.model.InpPhamApplyDetailSummaryModel;
import com.th.supcom.pds.phamapply.client.model.InpPhamApplyPatInfoModel;
import com.th.supcom.pds.phamapply.client.service.InpPhamApplyDetailRpc;

/**
 * InpPhamApplyDetail 控制层.
 * 
 * 住院摆药请领明细.
 */
public class InpPhamApplyDetailServiceController extends GwtController implements InpPhamApplyDetailRpc
{
    private static final long serialVersionUID = 1L;

    private PhamApplyService m_phamApplyService;
    private Mapper m_mapper;

    /**
     * 默认构造函数.
     */
    public InpPhamApplyDetailServiceController ()
    {

    }

    /**
     * 获取mapper
     * 
     * @param mapper the mapper to set.
     */
    public void setMapper (Mapper mapper)
    {
        m_mapper = mapper;
    }

    public void setPhamApplyService (PhamApplyService phamApplyService)
    {

        m_phamApplyService = phamApplyService;
    }

    @Override
    public InpPhamApplyDetailModel getInpPhamApplyDetailById (Long id) throws UiException
    {
        // TODO Controller层调用服务
        return null;
    }

    @Override
    public void addInpPhamApplyDetail (InpPhamApplyDetailModel inpphamapplydetailModel) throws UiException
    {
        // TODO Controller层调用服务
        // InpPhamApplyDetail inpphamapplydetail =
        // ConvertUtils.convert(InpPhamApplyDetail.class,
        // inpphamapplydetailModel);

    }

    @Override
    public void modifyInpPhamApplyDetail (InpPhamApplyDetailModel inpphamapplydetailModel) throws UiException
    {
        // TODO Controller层调用服务
        // InpPhamApplyDetail inpphamapplydetail =
        // ConvertUtils.convert(InpPhamApplyDetail.class,
        // inpphamapplydetailModel);

    }

    @Override
    public void removeInpPhamApplyDetail (Long id) throws UiException
    {
        // TODO Controller层调用服务
    }

    @Override
    public PagingLoadResult <InpPhamApplyDetailModel> getInpPhamApplyDetailListWithPaging (PagingLoadConfig config)
    {
        // TODO Controller层调用服务
        return new BasePagingLoadResult <InpPhamApplyDetailModel> (new ArrayList <InpPhamApplyDetailModel> (),
                                                                   config.getOffset (), 10);
    }

    @Override
    public List <InpPhamApplyPatInfoModel> getInpPhamApplyPatInfos (String nurseUnit)
    {
        List <InpPhamApplyPatInfo> patInfos=null;
        try
        {
            patInfos = m_phamApplyService.getApplyPatInfosByNurseUnit (nurseUnit);
        }
        catch (Exception e)
        {
            throw new BusinessException (e.getLocalizedMessage ());
        }
        if (patInfos.isEmpty ())
        {
            return new ArrayList <InpPhamApplyPatInfoModel> ();
        }
        return ConvertUtils.convert (InpPhamApplyPatInfoModel.class, patInfos);
    }

    @Override
    public List <InpPhamApplyDetailModel> findPhamApplyDetailInfo (List <InpPhamApplyPatInfoModel> inpPatInfoModels,
                                                                   String nurseUnit, Date applyDate, Date endDate,
                                                                   String applicant, String repetition)
    {
        InpPhamApplyMaster inpPhamApplyMaster = new InpPhamApplyMaster ();
        inpPhamApplyMaster.setApplyDatetime (applyDate);
        inpPhamApplyMaster.setEndDispenseDate (endDate);

        if (inpPatInfoModels.isEmpty ())
        {
            throw new BusinessException ("没有患者请领，请刷新患者信息列表再尝试！");
        }

        // 将患者住院摆药信息model转换成pojo
        List <InpPhamApplyPatInfo> inpPhamApplyPatInfos = new ArrayList <InpPhamApplyPatInfo> ();
        for (InpPhamApplyPatInfoModel model : inpPatInfoModels)
        {
            inpPhamApplyPatInfos.add (m_mapper.map (model, InpPhamApplyPatInfo.class));
        }

        inpPhamApplyMaster.setApplyBy (applicant);
        inpPhamApplyMaster.setNurseUnit (nurseUnit);
        List <InpPhamApplyDetailVO> list = m_phamApplyService.generatePhamApplyDetailList (inpPhamApplyPatInfos,
                                                                                           inpPhamApplyMaster,
                                                                                           repetition);

        if (list.isEmpty ())
        {
            return new ArrayList <InpPhamApplyDetailModel> ();
        }
        // 将结果转成model
        List <InpPhamApplyDetailModel> result = new ArrayList <InpPhamApplyDetailModel> ();
        for (InpPhamApplyDetailVO vo : list)
        {
            result.add (m_mapper.map (vo, InpPhamApplyDetailModel.class));
        }
        return result;
    }

    @Override
    public void saveInpPhamApplyDetailList (List <InpPhamApplyDetailModel> details, String nurseUnit, Date applyDate,
                                            Date endDate, String applicant) throws UiException
    {
        ArrayList <InpPhamApplyDetailVO> list = new ArrayList <InpPhamApplyDetailVO> ();
        for (InpPhamApplyDetailModel model : details)
        {
            InpPhamApplyDetailVO inpPhamApplyDetail = m_mapper.map (model, InpPhamApplyDetailVO.class);
            list.add (inpPhamApplyDetail);
        }

        InpPhamApplyMaster inpPhamApplyMaster = new InpPhamApplyMaster ();
        inpPhamApplyMaster.setApplyDatetime (applyDate);
        inpPhamApplyMaster.setEndDispenseDate (endDate);

        inpPhamApplyMaster.setApplyBy (applicant);

        inpPhamApplyMaster.setNurseUnit (nurseUnit);

        m_phamApplyService.savePhamApplyMasterAndDetail (inpPhamApplyMaster, list);
    }

    @Override
    public List <InpPhamApplyDetailModel> getOffsetPhamSummaryList (List <InpPhamApplyDetailSummaryModel> summaryList)
                                                                                                                      throws UiException
    {
        // 将患者住院摆药信息model转换成pojo
        List <InpPhamApplyDetailSummary> detailSummaryList = new ArrayList <InpPhamApplyDetailSummary> ();
        for (InpPhamApplyDetailSummaryModel model : summaryList)
        {
            detailSummaryList.add (m_mapper.map (model, InpPhamApplyDetailSummary.class));
        }

        List <InpPhamApplyDetailVO> details = m_phamApplyService.getOffsetPhamSummaryList (detailSummaryList);

        List <InpPhamApplyDetailModel> result = new ArrayList <InpPhamApplyDetailModel> ();
        for (InpPhamApplyDetailVO vo : details)
        {
            result.add (m_mapper.map (vo, InpPhamApplyDetailModel.class));
        }
        return result;
    }

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, TH Supcom Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
}